package com.huaxin.hxenum.enums;

import com.huaxin.hxenum.base.BaseCodeEnum;

/**
 * 星期枚举类
 * 第一天是星期日 (Sunday)，最后一天是星期六 (Saturday)
 */
public enum DayOfWeek implements BaseCodeEnum<Integer> {
    Sunday(1, "星期日"),
    Monday(2, "星期一"),
    Tuesday(3, "星期二"),
    Wednesday(4, "星期三"),
    Thursday(5, "星期四"),
    Friday(6, "星期五"),
    Saturday(7, "星期六");

    private final int code;
    private final String displayName;

    DayOfWeek(int code, String displayName) {
        this.code = code;
        this.displayName = displayName;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    /**
     * 根据 code 获取枚举实例
     */
    public static DayOfWeek of(int code) {
        for (DayOfWeek day : values()) {
            if (day.code == code) {
                return day;
            }
        }
        return null;
    }

    /**
     * 校验 code 是否有效
     */
    public static boolean isValid(int code) {
        return of(code) != null;
    }

    /**
     * 兼容字符串输入（用于校验）
     */
    public static boolean isValid(String codeStr) {
        if (codeStr == null || codeStr.trim().isEmpty()) {
            return false;
        }
        try {
            int code = Integer.parseInt(codeStr.trim());
            return isValid(code);
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
